#include "uvm.h"
#include "uvm_private.h"

static void on_async_cb(uv_async_t* handle) {
    uvm_impl_t *uvm = (uvm_impl_t*)handle->data;
    async_req_t *req;
    uv_mutex_lock(&uvm->mutex);
    req = uvm->async_req_head;
    if(uvm->async_req_head)
        uvm->async_req_head = uvm->async_req_head->next;
    uv_mutex_unlock(&uvm->mutex);
    if(req)
        req->cb((uvm_t*)uvm, req->data);
    free(req->data);
}

uvm_t* uvm_init(void *uv) {
    uvm_impl_t *ret = (uvm_impl_t*)calloc(1, sizeof(uvm_impl_t));
    if(uv) {
        ret->uv = (uv_loop_t*)uv;
    } else {
        ret->uv = (uv_loop_t*)calloc(1, sizeof(uv_loop_t));
        ret->inneruv = 1;
    }
    ret->async.data = ret;
    uv_async_init(ret->uv, &ret->async, on_async_cb);
    uv_mutex_init(&ret->mutex);

    return (uvm_t*)ret;
}

void uvm_clean(uvm_t* uvm) {
}

void uvm_async(uvm_t* uvm, uvm_async_cb cb, void* data) {
    uvm_impl_t *uvm_impl = (uvm_impl_t*)uvm;
    async_req_t *req = (async_req_t*)calloc(1, sizeof(async_req_t));
    req->data = data;
    req->cb = cb;
    uv_mutex_lock(&uvm_impl->mutex);
    if(uvm_impl->async_req_head == NULL) {
        uvm_impl->async_req_tail = uvm_impl->async_req_head = req;
    } else {
        uvm_impl->async_req_tail->next = req;
        uvm_impl->async_req_tail = req;
    }
    uv_mutex_unlock(&uvm_impl->mutex);
    uv_async_send(&uvm_impl->async);
}

char* url_encode(char *s, int len, int *new_length) {
    unsigned char *from, *end, *start, *to, c;
    unsigned char hexchars[] = "0123456789ABCDEF";
    from = (unsigned char*)s;
    end = from + len;
    start = to = (unsigned char *)malloc(3 * len + 1);


    while (from < end) {
        c = *from++;
        if (c == ' ') {
            *to++ = '+';
        } else if ((c < '0' && c != '-' && c != '.')
            ||(c < 'A' && c > '9')
            ||(c > 'Z' && c < 'a' && c != '_')
            ||(c > 'z')) {
                to[0] = '%';
                to[1] = hexchars[c >> 4];
                to[2] = hexchars[c & 15];
                to += 3;
        } else {
            *to++ = c;
        }
    }
    *to = 0;
    if (new_length) {
        *new_length = (int)(to - start);
    }
    return (char*)start;
}

int url_decode(char *str, int len) {
    char *dest = str;
    char *data = str;

    int value;
    int c;

    while (len--) {
        if (*data == '+') {
            *dest = ' ';
        }
        else if (*data == '%' && len >= 2 && isxdigit((int) *(data + 1))
            && isxdigit((int) *(data + 2)))
        {

            c = ((unsigned char *)(data+1))[0];
            if (isupper(c))
                c = tolower(c);
            value = (c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10) * 16;
            c = ((unsigned char *)(data+1))[1];
            if (isupper(c))
                c = tolower(c);
            value += c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10;

            *dest = (char)value ;
            data += 2;
            len -= 2;
        } else {
            *dest = *data;
        }
        data++;
        dest++;
    }
    *dest = 0;
    return (int)(dest - str);
}